<?php


namespace App\Service;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\FlyGourmetMealClassify;
use App\Model\Product;
use http\Exception;

/**
 *
 * 商户美食分类配置
 * Class FlyGourmetMealClassifyService
 * @package App\Service
 */
class FlyGourmetMealClassifyService extends BaseService
{

    /**
     * Notes:
     * @param array $param
     * @param int $shopId
     * @return object
     * Date: 2022/7/21
     * Time:17:31
     * Auth: zdl
     */
    public function index(array $param,int $shopId)
    {
        $this->init_class($shopId);
        $this_class =  New FlyGourmetMealClassify();
        return  $this_class->where('shop_id',$shopId)
            ->when($param['name'],function($query)use($param) {
              $query->where('name','like','%'.$param['name'].'%');
            })
            ->with('product_list')
            ->orderBy('is_init','DESC')
            ->select()
            ->get();
    }

    /**
     * Notes:
     * @param int $shopId
     * @return object
     * Date: 2022/7/21
     * Time:17:31
     * Auth: zdl
     */
    public function init_class(int $shopId)
    {
        $this_class =  New FlyGourmetMealClassify();
        $init_arr =   $this_class
            ->where('shop_id',$shopId)
            ->where('is_init','1')
            ->first();
        if(!$init_arr){
            return  $this_class->insertGetId([
                'is_init'=>1,
                'shop_id'=>$shopId,
                'name'=>'默认',
            ]);
        }else{
            return $init_arr->id;
        }
    }
    /**
     * 插入分类数据
     * add
     * @param int $shopId
     * @param string $name
     * @return int|Exception
     * date:2022/7/19
     * time:17:04
     * auth：xyc
     */
    public function add(int $shopId, string $name)
    {
        $this_class =  New FlyGourmetMealClassify();
        $model = $this_class->where('shop_id', $shopId)->where('name', $name)->first();
        if ($model != null) {
            throw new BusinessException(ErrorCode::SYSTEM_INVALID, "该类别已经存在了!");
        }
        $this_class->name = $name;
        $this_class->shop_id = $shopId;
        if (!$this_class->save()) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, "插入失败!");
        }
        return $this_class->id;
    }

    /**
     * 修改
     * update
     * @param int $id
     * @param string $name
     * date:2022/7/19
     * time:17:10
     * auth：xyc
     * @return bool
     */
    public function update(int $id, string $name)
    {
        $model = FlyGourmetMealClassify::query()->find($id);
        if ($model == null) {
            throw new BusinessException(ErrorCode::NOT_FOUND, "该类别不存在了!");
        }
        $classModel = FlyGourmetMealClassify::query()->where('shop_id', $model->shop_id)->where('name', $name)->first();
        if ($classModel != null && $classModel->id != $model->id) {
            throw new BusinessException(ErrorCode::SYSTEM_INVALID, "该类别已经存在了!");
        }
        if (!$model->fill(['name' => $name])->save()) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, "保存失败!");
        }else{
            return true;
        }
    }

    /**
     * 修改
     * update
     * @param int $id
     * date:2022/7/19
     * time:17:10
     * auth：xyc
     * @return bool
     * @throws \Exception
     */
    public function del(int $id)
    {
        $model = FlyGourmetMealClassify::query()->find($id);
        if ($model == null) {
            throw new BusinessException(ErrorCode::NOT_FOUND, "该类别不存在了!");
        }
        if ($model->is_init == 1) {
            throw new BusinessException(ErrorCode::NOT_FOUND, "默认分类不可删除!");
        }
//        该分类下所有商品更新
        Product::query()->where('category_id',$id)->update(['category_id'=>0]);
        if (!$model->delete()) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, "删除失败!");
        }else{
            return true;
        }
    }
 }